import Vue from 'vue'
import axios from 'axios'
import Router from 'vue-router'
import layout from '../views/layout/layout'

import login from '../views/entrance/login'
import home from '../views/home/home'
import invitationCode from '../views/invitationCode/invitationCode'
import merchant from '../views/merchant/merchant'
import shopTasks from '../views/shopTasks/shopTasks'
import shopHistoricalData from '../views/shopHistoricalData/shopHistoricalData'
import shopHistoricalAllData from '../views/shopHistoricalData/shopHistoricalAllData'
import promoterList from '../views/promoterList/promoterList'
import merchantOrderNumber from '../views/merchantOrderNumber/merchantOrderNumber'
import capitalFlow from '../views/capitalFlow/capitalFlow'
import promoteOrderNumber from '../views/promoteOrderNumber/promoteOrderNumber'
import commissionTable from '../views/commissionTable/commissionTable'
import saleTaskDetail from '../views/saleTaskDetail/saleTaskDetail'
import incomeList from '../views/incomeList/incomeList.vue'
import completeOrderDetail from '../views/completeOrderDetail/completeOrderDetail.vue'

import salesmanList from '../views/salesmanList/salesmanList.vue'
import indirectShopTasks from '../views/indirectShopTasks/indirectShopTasks.vue'

import costStrategy from '../views/costStrategy/costStrategy.vue'
import priceStrategy from '../views/priceStrategy/priceStrategy.vue'
import promoteStrategy from '../views/promoteStrategy/promoteStrategy.vue'
import merchantCharges from '../views/merchantCharges/merchantCharges.vue'

import shopList from '../views/shopList/shopList.vue'

import error1 from '../views/errorPage/error1.vue'
import error2 from '../views/errorPage/error2.vue'

import { apiIsLogin, apiMechantList } from '../request/api'
Vue.use(Router)
const router = new Router({
  // mode: 'history',// 强登陆必须选这种
  mode: 'hash',
  routes: [{
      path: '/',
      redirect: 'login'
    },{
    path: '/login',
    name: '登录',
    component: login,
    noMenu: true
  },{
    path: '/error1',
    name: '404',
    component: error1,
    noMenu: true
  }, {
    path: '/error2',
    name: '401',
    component: error2,
    noMenu: true
  },{
    // 首页
    path: '/home',
    component: layout,
    noMenu: false,
    children: [{
      path: '/home',
      name: '首页',
      component: home,
      meta: {
        requiresAuth: true,
        noChildren: true,
        isShow: true,
        menuIcon: 'el-icon-ali-lujing'
      }
    }, {
      path: '/invitationCode',
      name: '我的邀请码',
      component: invitationCode,
      meta: {
        requiresAuth: true,
        noChildren: true,
        isShow: true,
        menuIcon: 'el-icon-ali-invitation-code'
      }
    },{
      path: '/commissionTable',
      name: '我的成本方案',
      component: commissionTable,
      meta: {
        requiresAuth: true,
        noChildren: true,
        isShow: true,
        menuIcon: 'el-icon-ali-commission'
      }
    },{
      path: '/merchant',
      name: '商户列表',
      component: merchant,
      meta: {
        requiresAuth: true,
        noChildren: true,
        isShow: true,
        menuIcon: 'el-icon-ali-merchant'
      }
    },{
      path: '/merchantCharges',
      name: '商家收费方案',
      component: merchantCharges,
      meta: {
        requiresAuth: true,
        noChildren: true,
		    isShow: true,
        menuIcon: 'el-icon-ali-charges'
      }
    },{
      path: '/priceStrategy',
      name: '设置商家方案',
      component: priceStrategy,
      meta: {
        requiresAuth: true,
        noChildren: true,
		    isShow: true,
        menuIcon: 'el-icon-ali-price'
      }
    },
    {
      path: '/shopList',
      name: '店铺列表',
      component: shopList,
      meta: {
        requiresAuth: true,
        noChildren: true,
		    isShow: true,
        menuIcon: 'el-icon-ali-pormote-list'
      }
    },
    {
      path: '/promoteOrderNumber',
      name: '推广员列表',
      component: promoteOrderNumber,
      meta: {
        requiresAuth: true,
        noChildren: true,
		    isShow: false,
        menuIcon: 'el-icon-ali-orders-1'
      }
    },
    {
      path: '/costStrategy',
      name: '推广成本方案',
      component: costStrategy,
      meta: {
        requiresAuth: true,
        noChildren: true,
		    isShow: false,
        menuIcon: 'el-icon-ali-cost'
      }
    },{
      path: '/promoteStrategy',
      name: '设置推广方案',
      component: promoteStrategy,
      meta: {
        requiresAuth: true,
        noChildren: true,
		    isShow: false,
        menuIcon: 'el-icon-ali-promote-strategy'
      }
    },
    {
      path: '/saleTaskDetail',
      name: '销量任务明细',
      component: saleTaskDetail,
      meta: {
        requiresAuth: true,
        noChildren: true,
		    isShow: true,
        menuIcon: 'el-icon-ali-sales'
      }
    }, {
      path: '/completeOrderDetail',
      name: '完成订单明细',
      component: completeOrderDetail,
      meta: {
        requiresAuth: true,
        noChildren: true,
		    isShow: true,
        menuIcon: 'el-icon-ali-complete-order'
      }
    },
    {
      path: '/shopTasks',
      name: '店铺收益',
      component: shopTasks,
      meta: {
        requiresAuth: true,
        noChildren: true,
		    isShow: true,
        menuIcon: 'el-icon-ali-income'
      }
    },{
      path: '/indirectShopTasks',
      name: '推广员收益',
      component: indirectShopTasks,
      meta: {
        requiresAuth: true,
        noChildren: true,
		    isShow: false,
        menuIcon: 'el-icon-ali-income'
      }
    },
    {
      path: '/incomeList',
      name: '收益列表',
      component: incomeList,
      meta: {
        requiresAuth: true,
        noChildren: true,
		    isShow: true,
        menuIcon: 'el-icon-ali-income-list'
      }
    },
    {
      path: '/shopHistoricalData',
      name: '店铺历史数据',
      component: shopHistoricalData,
      meta: {
        requiresAuth: true,
        noChildren: true,
		    isShow: true,
        menuIcon: 'el-icon-ali-shop-history-data'
      }
    },
    {
      path: '/shopHistoricalAllData',
      name: '店铺任务进度概览',
      component: shopHistoricalAllData,
      meta: {
        requiresAuth: true,
        noChildren: true,
        isShow: true,
        menuIcon: 'el-icon-ali-shop-history-data'
      }
    },
    {
      path: '/capitalFlow',
      name: '资金流水',
      component: capitalFlow,
      meta: {
        requiresAuth: true,
        noChildren: true,
		    isShow: true,
        menuIcon: 'el-icon-ali-capital-flow'
      }
    }]
  }]
})
router.beforeEach((to, from, next) => {
  // console.log(to.query)
  if(to.query && to.query.token){
    window.sessionStorage.setItem('token',to.query.token)
    // console.log('强登')
    next('/home')
  } else {
    let token = window.sessionStorage.getItem("token")
    console.log('---token'+token)
    if (to.path === '/login' || to.path === '/error1' || to.path === '/error2') {
      // console.log('登录注册')
      next()
    }else {
      if (token){
        apiIsLogin({ 'token':token }).then(res =>{
          if(res.code == 1){
            next()
          }else {
            next('/login')
          }
        })
      }else {
        next('/login')
      }

    //   axios.post(
    //     "baseapi/token/verifyToken?token="+ token
    //   ).then((res) => {
    //     if(res.data.code == 1){
    //       // next()
    //     }else {
    //       // next('/login')
    //     }
    //   }).catch((error) => {
    //     console.log(error)
    //   })
    //   if(!token){
    //     console.log('没有token')
    //     // next('/login')
    //   } else {
    //     // console.log('有token')
    //     next ()
    //   }
    }
  }
})
export default router
